(function($){
    $(function(){
    	if(global.sysFid!=""){
    		$("#fileupload-progress").show();
    		$(".table").show();
    	}
    	$("#files").change(function(){
    		$("#fileupload-progress").show();
    		$(".table").show();
    	});
    	$(".cancel").click(function(){
    		$("#fileupload-progress").hide();
    		$(".table").hide();
    	});
    	$("#deleteAll").click(function(){
    		global.chkdCount = $(".files").find('.delete input:checked').length;
    		global.isDeleteAll = true;
    		global.isBatchDelBtn = true;
    	});
    	$("#close_btn").click(function(){
    		clearFids();
    		window.close();
    	});
    	KindEditor.ready(function(K) {
        	var editor = K.create('#content', {
				items : [
			  		'source', '|', 'undo', 'redo', '|', 'preview', 'print', 'template', 'cut', 'copy', 'paste',
					'plainpaste', 'wordpaste', '|', 'justifyleft', 'justifycenter', 'justifyright',
					'justifyfull', 'insertorderedlist', 'insertunorderedlist', 'indent', 'outdent', 'subscript',
					'superscript', 'clearhtml', 'quickformat', 'selectall', '|', 'fullscreen', '/',
					'formatblock', 'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold',
					'italic', 'underline', 'strikethrough', 'lineheight', 'removeformat', '|', 'image', 
					'flash', 'media', 'insertfile', 'table', 'hr', 'emoticons', 'baidumap', 'pagebreak',
					'anchor', 'link', 'unlink', '|', 'about'
				],
				uploadJson : window.basePath+'/document/art/upload_'+global.type+'.html',
				allowFileManager : false,
				allowImageUpload:true,
				allowFileUpload:false,
				allowMediaUpload:false,
				allowFlashUpload:false,
				urlType:'domain',
				afterChange:function(){
					this.sync();
				}});
    	});
    	var $form = $('#fileupload');
    	var $subbtn = $("#submit_btn");
    	$form.validator({
           theme:'yellow_right_effect',
           stopOnError: false,
           timely: 1,
           focusCleanup:true,
           fields: {
               'title': '标题:required;length[1~50];',
               'content': '内容:required;',
               'summary':'摘要:length[1~1000];',
               'expert':'专家名:required;',
               'position':'职称:required;'
           },
           valid: function(){
        	   if($("#file_upload-queue").html()){
        		   asyncbox.tips("请等待视频文件上传完成！",'error');
        		   return;
        	   }
        	   $subbtn.attr("disabled",true);
        	   $subbtn.val("正在保存，请稍候...");
        	   var d = $form.serialize();
        	   var url = $form.attr("subaction");
        	   $.post(url,d,function(r){
        	    asyncbox.tips(r.m,r.r==0 ? 'success' : 'error');
        	    if(r.r==0) {
        	    	fileUpload.isDel = false;
             	    fileUpload.del("save");
        	        setTimeout(function(){
        	            document.location.reload();
        	        }, 2000);
        	    }else{
        	 	   $subbtn.attr("disabled",false);
        	 	   $subbtn.val("确认保存");
        	    }
        	   },"json");
           }
        });
    });
})(jQuery);
var fillVal = function(result){
   if(result!=null){
	   var uploadFile = jQuery.parseJSON(result).d[0].videoUrlTmp;
	   $("#href-filename").attr("href",window.rootPath+"/admin/document/file-"+uploadFile.fileId+".do").html(uploadFile.remoteName);
	   $("input[name='videoUrl']").val(uploadFile.fileId);
	   fileUpload.delFile(uploadFile.fileId);
   }
};
var delSingle = function(){
	if(global.chkdCount==0){
		global.isDeleteAll = false;
		global.isBatchDelBtn = false;
	}else{
		global.chkdCount--;
	}
};
var clearFids = function(flag){
	$.post(window.basePath+"/document/clearFids.do?isDelFile="+flag);
};
window.onbeforeunload = function(){
	clearFids(global.aid==""?true:false);
}; 